package com.iotproject.eduservice.controller;


import com.iotproject.commonutils.R;
import com.iotproject.eduservice.entity.EduCourse;
import com.iotproject.eduservice.entity.vo.CourseInfoVo;
import com.iotproject.eduservice.entity.vo.CoursePublishVo;
import com.iotproject.eduservice.entity.vo.CourseQuery;
import com.iotproject.eduservice.service.EduChapterService;
import com.iotproject.eduservice.service.EduCourseDescriptionService;
import com.iotproject.eduservice.service.EduCourseService;
import com.iotproject.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-08
 */
@RestController
@RequestMapping("/eduservice/course")
//d @CrossOrigin
public class EduCourseController {
    @Autowired
    EduCourseService courseService;

    @Autowired
    EduVideoService videoService;

    @Autowired
    EduChapterService chapterService;

    @Autowired
    EduCourseDescriptionService courseDescriptionService;

    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后课程id，为了后面添加大纲使用
        String courseId = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", courseId);
    }

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //修改课程状态为——发布
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable("id") String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //条件查询——根据课程名称和发布状态查询
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable("current") long current,
                                  @PathVariable("limit") long limit,
                                  @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> coursePage = new Page<>(current, limit);
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();

        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        if (!StringUtils.isEmpty(title)) {
            courseQueryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            courseQueryWrapper.eq("status", status);
        }
        courseQueryWrapper.orderByDesc("gmt_create");
        courseService.page(coursePage, courseQueryWrapper);
        long total = coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    //根据课程id删除课程
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable("courseId") String courseId) {
        //删除小节
        videoService.deleteByCourseId(courseId);
        //删除章节
        chapterService.deleteByCourseId(courseId);
        //删除描述
        courseDescriptionService.removeById(courseId);
        //删除课程
        boolean res = courseService.removeById(courseId);
        if (res) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

